/**
 * @(#)ItemServiceImpl.java, 2018/2/5.
 * <p/>
 * Copyright 2018 Kerr Wang, Inc. All rights reserved.
 * KERRWANG PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package org.wangzx.exercise.service.basic.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wangzx.exercise.enums.BuyStateType;
import org.wangzx.exercise.enums.LoginType;
import org.wangzx.exercise.exception.CommonServiceException;
import org.wangzx.exercise.meta.bean.ItemBean;
import org.wangzx.exercise.meta.bean.OrderCartBean;
import org.wangzx.exercise.meta.bean.UserBean;
import org.wangzx.exercise.meta.convertor.CommonConvertor;
import org.wangzx.exercise.meta.vo.ItemVO;
import org.wangzx.exercise.meta.vo.UserVO;
import org.wangzx.exercise.service.basic.ItemService;
import org.wangzx.exercise.service.core.ItemCoreService;
import org.wangzx.exercise.service.core.OrderCartCoreService;
import org.wangzx.exercise.service.core.UserCoreService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hzwangzuxiang(ahscwzx@163.com)
 */
@Service("itemService")
public class ItemServiceImpl implements ItemService {
    static private final Logger logger = LoggerFactory.getLogger(ItemServiceImpl.class);

    @Autowired
    private ItemCoreService itemCoreService;

    @Autowired
    private UserCoreService userCoreService;

    @Autowired
    private OrderCartCoreService orderCartCoreService;

    @Override
    public List<ItemVO> getAllItemVO(UserVO userVO) {
        if (userVO.getLoginType() == LoginType.BUYER.getValue()) {
            UserBean userBean = userCoreService.getByName(userVO.getUserName());
            List<ItemVO> result = CommonConvertor.boToVo(itemCoreService.getAllItems(), ItemVO.class);
            for (ItemVO vo: result) {
                OrderCartBean bean = orderCartCoreService.getByBuyerAndItemId(userBean.getId(), vo.getId());
                if(bean != null && bean.getState() == BuyStateType.BUY) {
                    vo.setCount(bean.getCount());
                    vo.setPrePrice(bean.getPrice());
                }
            }
            return result;
        } else if (userVO.getLoginType() == LoginType.SELLER.getValue()) {
            UserBean userBean = userCoreService.getByName(userVO.getUserName());
            return CommonConvertor.boToVo(itemCoreService.getAllItemsBySeller(userBean.getId()), ItemVO.class);
        } else {
            return CommonConvertor.boToVo(itemCoreService.getAllItems(), ItemVO.class);
        }
    }

    @Override
    public long insertItem(ItemVO itemVO, long sellerId) {
        if (itemVO == null) {
            logger.error("[op:insertItem] itemVO is null");
            throw new CommonServiceException();
        }
        ItemBean bean = CommonConvertor.voToBo(itemVO, ItemBean.class);
        bean.setPublisher(sellerId);
        long id = itemCoreService.insertItem(bean);
        if (id == -1) {
            logger.error("[op:insertItem] insert error");
            throw new CommonServiceException();
        }
        return id;
    }

    @Override
    public ItemVO getById(long id, UserVO userVO) {
        ItemBean bean = itemCoreService.getItemById(id);
        ItemVO vo = CommonConvertor.boToVo(bean, ItemVO.class);

        if(userVO.getLoginType() == LoginType.BUYER.getValue()) {
            UserBean userBean = userCoreService.getByName(userVO.getUserName());
            if(userBean == null) {
                return vo;
            }
            OrderCartBean orderCartBean = orderCartCoreService.getByBuyerAndItemId(userBean.getId(), id);
            if(orderCartBean != null && orderCartBean.getState() == BuyStateType.BUY) {
                vo.setPrePrice(orderCartBean.getPrice());
                vo.setCount(orderCartBean.getCount());
            }
        }
        return vo;
    }

    @Override
    public List<ItemVO> getNotBuyItems(long buyerId) {
        List<ItemVO> list = new ArrayList<>();
        List<ItemBean> beans = itemCoreService.getAllItems();
        for (ItemBean bo: beans) {
            OrderCartBean bean = orderCartCoreService.getByBuyerAndItemId(buyerId, bo.getId());
            if(bean == null || bean.getState() == BuyStateType.CART) {
                list.add(CommonConvertor.boToVo(bo, ItemVO.class));
            }
        }
        return list;
    }

    @Override
    public boolean updateItem(ItemVO itemVO) {
        return itemCoreService.updateItem(CommonConvertor.voToBo(itemVO, ItemBean.class));
    }
}
